package com.deyuanyun.pic.ctrl.validatefun;

import com.deyuanyun.pic.common.util.DateUtil;
import com.deyuanyun.pic.common.validator.annotations.FiledDatehms;
import com.deyuanyun.pic.common.validator.vinterface.ValidateInterface;
import org.apache.commons.beanutils.BeanUtils;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.text.ParseException;

/**
 * 检测那种文本格式的日期，同时转换成我们需要的格式
 *
 * @author scalpel.王敏
 * @version [v1.0] 创建时间（2016/10/14）
 * @see // 引用的类
 * Method List:
 * 1.----------------
 * 2.----------------
 * History:
 */
public class CustomDateValid implements ValidateInterface {
    @Override
    public <T, E, C> boolean apply(T t, E e, C c) throws NoSuchFieldException {
        Field f=(Field) e;
        String val= (String) t;
        FiledDatehms datehms = f.getAnnotation(FiledDatehms.class);
        if(datehms != null)
        {
            try {
                Date date = DateUtil.convertStringToDate(val,datehms.format());
                val = DateUtil.DateTimeToString(date);
                BeanUtils.setProperty(c,f.getName(),val);
                return true;
            } catch (ParseException e1) {
                e1.printStackTrace();
            } catch (InvocationTargetException e1) {
                e1.printStackTrace();
            } catch (IllegalAccessException e1) {
                e1.printStackTrace();
            }
            return false;
        }
        return true;
    }

    @Override
    public String getName() {
        return null;
    }
}
